﻿using Common.Prism.Apps;
using Common.Prism.Events;
using NxChatQQ.Models.Events;
using NxChatQQ.Services;
using Prism.Events;
using Prism.Regions;
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
using Wpf.Ui.Controls;

namespace NxChatQQ.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : FluentWindow
    {

        private Storyboard dialogAnimIn;
        private Storyboard dialogAnimOut;

        private readonly IRegionManager _regionManager;
        private readonly SimpleSnackbarService _SnackbarService;
        public MainWindow(IRegionManager regionManager, IEventAggregator eventAggregator, SimpleSnackbarService snackbarService)
        {
            InitializeComponent();
            //this.Loaded += MainWindow_Loaded;

            _SnackbarService = snackbarService;

            RegionManager.SetRegionManager(this, regionManager);
            RegionManager.UpdateRegions();
            _regionManager = regionManager;

            snackbarService.SetSnackbarPresenter(SnackbarPresenter);

            eventAggregator.GetEvent<DialogEvent>().Subscribe(OnNotifyMainModelView);
            eventAggregator.GetEvent<ShortTipEvent>().Subscribe(OnNotifyShortTip, ThreadOption.UIThread);

            this.Loaded += MainWindow_Loaded;

        }



        private void FluentWindow_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Global.EventAggregator.GetEvent<MouseButtonArgsEvent>().Publish(e);
        }


        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.BorderThickness = new Thickness(0);

            //////右键菜单--退出菜单项
            //var exit = new System.Windows.Controls.MenuItem();
            //exit.Name = "Quit";

            ////关联托盘控件
            //notifyIcon.ContextMenu = new ContextMenu();
            //notifyIcon.ContextMenu.Items.Add(exit);


            this.dialogAnimIn = this.FindResource("DialogPopupOpacityIn") as Storyboard;
            this.dialogAnimOut = this.FindResource("DialogPopupOpacityOut") as Storyboard;
        }


        #region 模态框
        private void OnNotifyShortTip(ShortTipData data)
        {
            switch (data.Appearance)
            {
                case TipType.Info:
                    _SnackbarService.ShowInfo(data.Message);
                    break;
                case TipType.Success:
                    _SnackbarService.ShowSuccess(data.Message);
                    break;
                case TipType.Error:
                    _SnackbarService.ShowError(data.Message);
                    break;
                case TipType.Warning:
                    _SnackbarService.ShowWarn(data.Message);
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 模态框

        public bool AllowClose { get; set; }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // 看看是否能关闭提示框
            if (AllowClose)
            {
                OnNotifyMainModelView(null);
            }
        }


        public void OnNotifyMainModelView(DialogData content)
        {
            if (content != null)
            {
                AllowClose = content.AllowClose;
                mDialogGrid.Visibility = Visibility.Visible;
                _regionManager.RequestNavigate("DialogHostContent", content.ViewName, content.Parameters);

                // 播放动画
                dialogAnimIn.Begin();
            }
            else
            {
                dialogAnimOut.Begin();
            }
        }


        private void DoubleAnimation_Completed(object sender, EventArgs e)
        {
            mDialogGrid.Visibility = Visibility.Collapsed;
            _regionManager.Regions["DialogHostContent"]?.RemoveAll();
        }

        #endregion
    }
}